from django.shortcuts import render

# Create your views here.
from django.views import View

from apps.contents.models import ContentCategory
from apps.contents.utils import get_categories


class IndexView(View):
    def get(self, request):
       #1.商品分类，数据显示
       categories = get_categories()
       #2.广告数据显示
       contents = {}
        #2.1获取广告分类
       ad_categories = ContentCategory.objects.all()
       #2.2遍历广告分类---找到广告对应的内容e
       for ad_cat in ad_categories:                           #status表示是否还存在
           #ad_cat.key通过不同的类别赋予不同的内容
           contents[ad_cat.key] = ad_cat.content_set.filter(status = True).order_by("sequence")

       context = {
           'categories': categories,
           'contents':contents
       }
       # print(context)
       return render(request, 'index.html',context)